package com.yhy;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.Scanner;

public class HelloServer {
    private int PORT = 10000;
    //接收请求的 nio 池
    private EventLoopGroup bossGroup = new NioEventLoopGroup();
    //接收数据的 nio 池
    private EventLoopGroup workerGroup = new NioEventLoopGroup();


    public static void main( String args[] ){
        HelloServer helloServer = new HelloServer();
        try {
            helloServer.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Scanner in=new Scanner(System.in); //使用Scanner类定义对象
        in.next();

        helloServer.stop();
    }

    public void start() throws InterruptedException {
        ServerBootstrap b = new ServerBootstrap();
        //指定接收链接的 NioEventLoop,和接收数据的 NioEventLoop
        b.group(bossGroup, workerGroup);
        //指定server使用的 channel
        b.channel(NioServerSocketChannel.class);
        //初始化处理请求的编解码，处理响应类等
        b.childHandler(new HelloServerInitializer());
        // 服务器绑定端口监听
        b.bind(PORT).sync();
    }

    public void stop(){
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
